package enemies 
{
	import collisions.World;
	import entities.Entity;
	import flash.geom.Point;
	/**
	 * ...
	 * @author Gareth Williams
	 * base class for ai behaviours
	 */
	public class Strategy
	{
		public var implementation:IStrategyImplementation = null;
		private var _isCompleted:Boolean = false;
		public function perform(on:Entity):Boolean
		{
			_isCompleted = implementation.perform(on);
			return _isCompleted;
		}
		public function get strategyIsComplete():Boolean
		{
			return _isCompleted
		}
		//helpers 
		//not massively efficient but this is what happens with Inversion of Control without dependency injection...
		private function findInWorld(who:Entity):Entity
		{
			for each (var body in World.bodies)
			{
				if (body as Entity === who) return Entity(who);
			}
		}
		private function checkWhetherReached(target:Point):Boolean
		{
			var reachedTarget:Boolean = false;
			//todo: would be nicer to use an entity and leverage the collision engine
			var euclidianDistance:Number = Point.distance(this, target);
			if (euclidianDistance < Constants.MINIMUM_QUADTREE_AREA) reachedTarget = true;
			return reachedTarget;
		}
		private function moveForward(on:Entity):void
		{
			on.x += Math.cos(on.rotation) * speed;
			on.y += Math.sin(on.rotation) * speed;
		}
		private function rotateTo(on:Entity, target:Entity):void
		{
			var delta:Point = Point(target).subtract(on as Point);
			var angleToTarget:Number = Math.atan2(delta.y, delta.x);
			on.rotation = angleToTarget;
		}
	}
}